package edu.mse.camel.client.whiteboard.gef.ui.commands;


import edu.mse.camel.client.whiteboard.gef.whiteboard.FreeWritingShape;
import edu.mse.camel.client.whiteboard.gef.whiteboard.WhiteboardDiagram;

/**
 * A command to remove a shape from its parent.
 * The command can be undone or redone.
 * @author choi
 */
public class FreeWritingShapeDeleteCommand extends AbstractWhiteboardCommand {
	/** Shape to remove. */
	private final FreeWritingShape child;	
	/** ShapeDiagram to remove from. */
	private final WhiteboardDiagram parent;
	/** Holds a copy of the outgoing connections of child. */
	
	private boolean wasRemoved;
	
	/**
	 * Create a command that will remove the shape from its parent.
	 * @param parent the ShapesDiagram containing the child
	 * @param child    the Shape to remove
	 * @throws IllegalArgumentException if any parameter is null
	 */
	public FreeWritingShapeDeleteCommand(WhiteboardDiagram parent, FreeWritingShape child) {
		if (parent == null || child == null) {
			throw new IllegalArgumentException();
		}
		setLabel("FreeWritingshape deletion");
		this.parent = parent;
		this.child = child;
	}
	
	/* (non-Javadoc)
	 * @see org.eclipse.gef.commands.Command#canUndo()
	 */
	public boolean canUndo() {
		return wasRemoved;
	}
	
	/* (non-Javadoc)
	 * @see org.eclipse.gef.commands.Command#execute()
	 */
	public void execute(){
		System.out.println("OK");
		wasRemoved = parent.removeChild(child);
	}
	
	/* (non-Javadoc)
	 * @see org.eclipse.gef.commands.Command#redo()
	 */
	
	
	
	public void redo() {
		// remove the child and disconnect its connections
		wasRemoved = parent.getFreeWritingShapes().contains(child);
		if (wasRemoved) {
			parent.getFreeWritingShapes().remove(child);
		}
	}
	

	/* (non-Javadoc)
	 * @see org.eclipse.gef.commands.Command#undo()
	 */
	public void undo() {
		if (parent.getFreeWritingShapes().add(child)) {
			wasRemoved = false;
		}
	}
}